package com.yzt.zhuju.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.yzt.zhuju.event.BaseBusEvent;
import com.yzt.zhuju.event.RxBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;

public abstract class RxBaseDialog extends DialogFragment {

    protected static HashMap<String, RxBaseDialog> mDialogFragments = new HashMap<String, RxBaseDialog>();

    protected DialogFragmentResultListener mDialogFragmentResultListener = new DialogFragmentResultListener() {
        @Override
        public void confirm(String[] strings) {
        }

        @Override
        public void cancel(String[] strings) {
        }
    };
    private final List<Subscription> mSubscriptions = new ArrayList<>();
    protected List<Subscriber> mSubscriberList = new ArrayList<>();

    protected DialogFragmentSetText mDialogFragmentSetText;
    protected DialogFragmentSetText1 mDialogFragmentSetText1;
    protected DialogFragmentSetImage mDialogFragmentSetImage;
    protected DialogFragmentSetView mDialogFragmentSetView;

    protected HashMap<String, TextView> mTextMaps = new HashMap<>();
    protected HashMap<String, ImageView> mImageMaps = new HashMap<>();
    protected HashMap<String, View> mViewMaps = new HashMap<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mSubscriberList == null) {
            mSubscriberList = new ArrayList<>();
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mDialogFragmentSetText != null) {
            mDialogFragmentSetText.customDialogText(mTextMaps);
        }
        if (mDialogFragmentSetImage != null) {
            mDialogFragmentSetImage.customDialogImage(mImageMaps);
        }
        if (mDialogFragmentSetView != null) {
            mDialogFragmentSetView.customDialogView(mViewMaps);
        }
        if (mDialogFragmentSetText1 != null) {
            mDialogFragmentSetText1.customDialogText(mTextMaps, mViewMaps);
        }
    }

    protected <E extends BaseBusEvent> void observerRxBus(Class<E> busClass, final Action1<E> onNext) {
        Subscription subscription = RxBus.getDefault().observerRxBus(busClass, onNext);
        mSubscriptions.add(subscription);
    }

    protected <E extends BaseBusEvent> void observerRxBusSticky(Class<E> busClass, final Action1<E> onNext) {
        Subscription subscription = RxBus.getDefault().observerRxBusSticky(busClass, onNext);
        mSubscriptions.add(subscription);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        RxBaseDialog dialogFragment = mDialogFragments.get(RxBaseDialog.this.getClass().getName());
        if (dialogFragment != null) {
            mDialogFragments.remove(RxBaseDialog.this.getClass().getName());
        }
    }

    public static RxBaseDialog showDialog(FragmentActivity context, Class<? extends RxBaseDialog> dialogClass) {
        return showDialog(context, dialogClass, null);
    }

    public static RxBaseDialog showDialog(FragmentActivity context, Class<? extends RxBaseDialog> dialogClass, Bundle args) {
        RxBaseDialog dialogFragment = null;
        if (context != null && !context.isFinishing() && !context.isDestroyed() && !context.getSupportFragmentManager().isStateSaved()) {
            try {
                dialogFragment = dialogClass.newInstance();
                if (args != null) {
                    dialogFragment.setArguments(args);
                }
                dialogFragment.show(context.getSupportFragmentManager(), dialogClass.getName());

                RxBaseDialog historyDialogFragment = mDialogFragments.get(dialogClass.getName());
                if (historyDialogFragment != null) {
                    historyDialogFragment.dismissAllowingStateLoss();
                    mDialogFragments.remove(dialogClass.getName());
                }
                mDialogFragments.put(dialogClass.getName(), dialogFragment);
            } catch (java.lang.InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return dialogFragment;
    }

    public void dismissDialog(String key) {
        RxBaseDialog dialogFragment = mDialogFragments.get(key);
        if (dialogFragment != null) {
            dialogFragment.dismissAllowingStateLoss();
            mDialogFragments.remove(key);
        } else {
            dismissAllowingStateLoss();
        }
    }

    @Override
    public void dismiss() {
        dismissDialog(this.getClass().getName());
    }

    /**
     * 对话框设置文本接口
     *
     * @author lxq_x
     */
    public interface DialogFragmentSetText {
        void customDialogText(HashMap<String, TextView> textViewMap);
    }

    public interface DialogFragmentSetText1 {
        void customDialogText(HashMap<String, TextView> textViewMap, HashMap<String, View> wMap);
    }

    /**
     * 对话框设置图片接口
     *
     * @author lxq_x
     */
    public interface DialogFragmentSetImage {
        void customDialogImage(HashMap<String, ImageView> imageViewMap);
    }

    /**
     * 对话框设置其他view内容
     *
     * @author lxq_x
     */
    public interface DialogFragmentSetView {
        void customDialogView(HashMap<String, View> viewMap);
    }

    /**
     * 对话框操作结果接口
     *
     * @author lxq_x
     */
    public interface DialogFragmentResultListener {
        void cancel(String... strings);

        void confirm(String... strings);
    }

    /**
     * 自定义对话框文本
     */
    public void setOnCustomDialogText(DialogFragmentSetText setTextListener) {
        mDialogFragmentSetText = setTextListener;
    }

    /**
     * 自定义对话框文本  含视频播放
     *
     * @param setTextListener
     */
    public void setOnCustomDialogText1(DialogFragmentSetText1 setTextListener) {
        mDialogFragmentSetText1 = setTextListener;
    }

    /**
     * 自定义对话框图片
     *
     * @param setImageListener
     */
    public void setOnCustomDialogImage(DialogFragmentSetImage setImageListener) {
        mDialogFragmentSetImage = setImageListener;
    }

    /**
     * 自定义对话框其他View内容
     *
     * @param setViewListener
     */
    public void setOnCustomDialogView(DialogFragmentSetView setViewListener) {
        mDialogFragmentSetView = setViewListener;
    }

    /**
     * 监听对话框操作结果
     *
     * @param resultListener
     */
    public void setOnDialogFragmentResultListener(DialogFragmentResultListener resultListener) {
        mDialogFragmentResultListener = resultListener;
    }

    /**
     * 在onStart方法中调用
     */
    // 设置dialog透明
    protected void setTransParentDialog() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.dimAmount = 0;
            lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            window.setAttributes(lp);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        for (Subscription subscription : mSubscriptions) {
            if (subscription == null) {
                continue;
            }
            if (subscription.isUnsubscribed()) {
                continue;
            }
            subscription.unsubscribe();
        }
        mSubscriptions.clear();

        for (Subscriber s : mSubscriberList) {
            if (!s.isUnsubscribed()) {
                s.unsubscribe();
            }
        }
        mSubscriberList.clear();
    }
}
